#This is the page for topic 7m # generate the data source("../gnrnd4.R") gnrnd4(1954287804,8500566) # be sure that we have the right data L1 # then, just because we know how to do it, # we will make a histogram of the frequencies hist( L1, main="Data from gnrnd4(1954287804,8500566)", xlab="Intervals across the data set", ylab="Frequency", ylim=c(0,25)) abline(h=0) abline(h=seq(5,25,5), lty="dotted") # We will take a clue from the histogram and # decide to have intervals of width 5 starting # at 35 and going up to 80. At this point # we will want to mimic the histogram by having # the intervals closed on the right. # # to do this we will set up our break points. b_pnts <- seq( 35, 80, 5) b_pnts # then we can use the cut() function to get the # interval into which each value in L1 falls which_interval <- cut( L1, b_pnts) which_interval # Now we have changed our problem from having # continuous values as in L1 to having discrete # values as in which_interval. Therefore we can # apply the same techniques that we used for # the discrete case. # freqs <- table( which_interval ) freqs # to compute the relative frequency we divide # the frequencies by the total number of items total <- length(L1) rel_freq <- freqs/total rel_freq # to compute the cumulative frequencies we # use the cumsum() function cum_count <- cumsum( freqs) cum_count # to compute the cumulative relative # frequencies we just divide the cumulative # frequencies by the total number of items cum_rel_freq <- cum_count/total cum_rel_freq # to compute the degrees to allocate in a pie # chart we just multiply the relative frequency # times 360 deg_pie <- 360*rel_freq deg_pie ############################## # But we captured all that in a function so use it ############################### # source("../make_freq_table.R") make_freq_table( which_interval ) #### there is one more special feature that we #### should see here. We can get a nicer looking #### table by using the View() function View( make_freq_table( which_interval ) ) ############################## # Converting this new problem to an old one # is a good problem solving technique. But # the process of conversion for this type # of problem is always the same. So, we might # as well incorporate all of this into a new # function. That function is collate3(). source("../collate3.R") # First, see what happens if we just try to # use collate3 with L1 collate3(L1) # The function ran but it did not give us the # output we expected. Instead it told us the # lowest and highest values in the data set and # then made recommendations about the width of # the interval. Using that information we may want # set the intervals to start at 35 and to have # a width of 5. collate3( L1, use_low=35, use_width=5 ) # that produced a nice frequency table, one that # even included a column for the midpoint of each # interval. ##################### # One feature that we did not cover was how to # get the intervals to be closed on the left. # Back at line 28 when we used the cut() # function we could have added the option # right=FALSE to force the intervals to be # closed on the left. which_interval <- cut( L1, b_pnts, right=FALSE) which_interval # And, we can do the same thing with collate3 collate3( L1, use_low=35, use_width=5, right=FALSE ) # note the change in the interval from 55 to 60.